<template>
  <el-card>
    <div class="flex justify-between items-center mb-20px">
      <h4>查看详情</h4>
      <el-button
        @click="
          () => {
            $router.back()
            delVisitedView(route)
          }
        "
        >返回</el-button
      >
    </div>
    <el-row>
      <el-col :span="2"> 信息ID： </el-col>
      <el-col :span="10">{{ formData.id }}</el-col>
    </el-row>
    <el-row class="mt-10px">
      <el-col :span="2"> 标题： </el-col>
      <el-col :span="10">{{ formData.title }}</el-col>
    </el-row>
    <el-row class="mt-10px">
      <el-col :span="2"> 信息类型： </el-col>
      <el-col :span="10">{{ formData.infoType }}</el-col>
    </el-row>
    <el-row class="mt-10px">
      <el-col :span="2"> 行业： </el-col>
      <el-col :span="10">{{ formData.category }}</el-col>
    </el-row>
    <el-row class="mt-10px">
      <el-col :span="2"> 地区： </el-col>
      <el-col :span="10">{{ formData.area }}</el-col>
    </el-row>
    <el-row class="mt-10px">
      <el-col :span="2"> 公布时间： </el-col>
      <el-col :span="10">{{ formData.publishDate }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 正文： </el-col>
      <el-col :span="10">
        <div v-html="formData.description"></div>
      </el-col>
    </el-row>
    <!-- <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="第三方id" prop="thirdPartyId">
        <el-input v-model="formData.thirdPartyId" placeholder="请输入第三方id" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="信息类型" prop="infoType">
        <el-select v-model="formData.infoType" placeholder="请选择信息类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="行业" prop="category">
        <el-input v-model="formData.category" placeholder="请输入行业" />
      </el-form-item>
      <el-form-item label="地区" prop="area">
        <el-input v-model="formData.area" placeholder="请输入地区" />
      </el-form-item>
      <el-form-item label="公布时间" prop="publishDate">
        <el-date-picker
          v-model="formData.publishDate"
          type="date"
          value-format="x"
          placeholder="选择公布时间"
        />
      </el-form-item>
      <el-form-item label="正文" prop="description">
        <Editor v-model="formData.description" height="150px" />
      </el-form-item>
    </el-form>
    <div>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </div> -->
  </el-card>
</template>
<script setup lang="ts">
import { ThirdPartyInformationApi, ThirdPartyInformation } from '@/api/biz/thirdpartyinformation'
import { useTagsViewStore } from '@/store/modules/tagsView'

const { delVisitedView } = useTagsViewStore()
const route = useRoute()

/** 第三方信息管理 表单 */
defineOptions({ name: 'ThirdPartyInformationForm' })

const id = +useRoute().params.id

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  thirdPartyId: undefined,
  title: undefined,
  infoType: undefined,
  category: undefined,
  area: undefined,
  publishDate: undefined,
  description: undefined
})
const formRules = reactive({
  description: [{ required: true, message: '正文不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ThirdPartyInformationApi.getThirdPartyInformation(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ThirdPartyInformation
    if (formType.value === 'create') {
      await ThirdPartyInformationApi.createThirdPartyInformation(data)
      message.success(t('common.createSuccess'))
    } else {
      await ThirdPartyInformationApi.updateThirdPartyInformation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    thirdPartyId: undefined,
    title: undefined,
    infoType: undefined,
    category: undefined,
    area: undefined,
    publishDate: undefined,
    description: undefined
  }
  formRef.value?.resetFields()
}

open('update', id)
</script>
